#version 330 core

in vec3 color;
in vec4 vEyePos;
in vec3 vEyeNormal;
in vec3 eyeLightDir;
in vec3 eyeHalfVector;

struct point_light
{
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    vec3 position;
};

struct material_properties
{
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    float shiness;
};

uniform point_light plight;
uniform material_properties material;

uniform bool hasMaterial;
uniform float alpha;


void main(void)
{
    vec3 L = normalize(eyeLightDir);
    vec3 N = normalize(vEyeNormal);
    vec3 V = normalize(-vEyePos.xyz);
    vec3 H = normalize(eyeHalfVector);

    vec3 ambient,diffuse,specular;
    float diff = max(dot(L,N),0.0);
    float spec = max(dot(H,N),0.0);

    if(hasMaterial){
        ambient = (plight.ambient*material.ambient).xyz;
        diffuse = diff * plight.diffuse.xyz * material.diffuse.xyz;
        specular = pow(spec,material.shiness*3.0) * plight.specular.xyz * material.specular.xyz;
    }else{
        ambient = plight.ambient.xyz*color;
        diffuse = diff * plight.diffuse.xyz*color;
        specular = pow(spec,128.0)*plight.specular.xyz*color;
    }
    gl_FragColor = vec4(ambient+diffuse+specular,alpha);
}
